# coding: utf-8

import time
import subprocess
import socket
import re



def get_Banner(Host,Port):
    try:
        socket.setdefaulttimeout(10)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # TCP
        s.connect((Host, int(Port)))
    except Exception as e:
        print(e)

    try:
        if int(Port) in (80,8080,443,8443,7001):
            payload = "GET / HTTP/1.1\r\n\r\n"
            s.sendall(bytes(payload, encoding='utf-8'))
            res = s.recv(1024)
            print("socket res:",res)
            s = re.search(r'Server:(.*?)\\r\\n', str(res))
            banner = s.group().split(':')[1].replace('\\r\\n', '')
            return banner
        else:
            banner = s.recv(1024)
            banner = banner.strip().decode('utf-8')
            return banner

    except Exception as e:
        return("unKnow")


if __name__ == '__main__':
    banner = get_Banner("www.baidu.com","80")
    print(banner)
    TIMESTAMP = time.strftime("%Y-%m-%d", time.localtime())
    print(TIMESTAMP)
    child = subprocess.Popen('ping   www.baidu.com',shell=True)
    child.wait()
